/**
 * 
 */
package com.base.myproject.client.busi.service.analysis;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.ColumnInfo;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.SystemMessage;
import com.base.myproject.client.ui.BaseBusiLeftPanel;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.celleditor.BaseGridEditor;
import com.base.myproject.client.ui.celleditor.CellEditorFactory;
import com.base.myproject.client.ui.celleditor.GridEditor;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.AggregationRowConfig;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.grid.SummaryType;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.ui.Label;

/**
 * @author zgw
 *2009-11-17 上午11:38:09
 * javazgw@gmail.com
 */
public class ServiceListAnalysis extends ContentPanel{
	BaseStore<BusiObject> store = null;
	BaseStore<BusiObject> s_service_po_g_store ;
	final NumberFormat currency = NumberFormat.getCurrencyFormat();
	final NumberFormat number = NumberFormat.getFormat("0.00");
	VerticalPanel vpanel;
	TextField<String> s_cardno;
	//BaseComboBoxForm<ModelData> ccode ;
	TextField<String> ccode;
	TextField<String> roomno ;
	
	Label aslabel;
	CheckBox check_isas ;
	
	BaseFormPanel bfp;
	
	TextField<String> html=new TextField<String>();
	
	BaseStore bcode_rander_store;
	NumberField Accounts ;
	NumberField Paid ;
	DateField servicestartdate ;
	DateField serviceenddate;
	NumberField peoplenum ;
	BaseGrid s_service_po_g_grid ;
	ContentPanel service_panel ;
	public ServiceListAnalysis()
	{
		init();
	}
	void init()
	{
		this.setWidth("1024px");
		//this.setHeight("720px");
		this.setAutoHeight(true);
		
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(5, 5, 0, 0));
		this.add(initWeiget(), centerData);

	}
	public ContentPanel initWeiget() {
		ContentPanel panel = new ContentPanel();
		panel.setHeaderVisible(false);
		panel.setLayout(new BorderLayout());
		BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST, 250);
		westData.setSplit(true);
		westData.setCollapsible(true);
		westData.setMargins(new Margins(10));
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(10));
		
		ContentPanel cpleft = CreateCcodeLeftPanel();
		
		
		panel.add(cpleft, westData);
		//CreateCcodeLeftPanel();
		 vpanel = new VerticalPanel();
//		 AssociatorPanel associatorpanel = new AssociatorPanel();
		
//		 vpanel.add(associatorpanel);
		vpanel.add(createService());
		vpanel.add(createServiceItem());
		
		panel.add(vpanel, centerData);
		//panel.setAutoWidth(true);
		panel.setHeight("480");
		//----------
		//panel.add(vpanel, westData);
		//BaseMainPanel.getInstance().setHeight("760");
		return panel;
	}
	
	public ContentPanel CreateCcodeLeftPanel() {
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		ColumnConfig icode = new ColumnConfig("icode", "单号", 50);
		
	//	columns.add(icode);
//		columns.add(new ColumnConfig("ccode	", "ccode", 50));
//		columns.add(new ColumnConfig("roomno", "roomno", 50));
		ColumnConfig busistatus = new ColumnConfig("busistatus", "状态", 50);
		
		GridCellRenderer<ModelData>	renderer = new GridCellRenderer<ModelData>() {

		      public String render(ModelData model, String property, ColumnData d, int rowIndex,
		          int colIndex, ListStore<ModelData> store, Grid<ModelData> grid) {

		       if(model.get(property)==null)
		        return "" ;

		       if(model.get(property).equals("524288"))
		           return "" + ("<img src='images/baseimage/money_yen.png' />" );
//		       else if(model.get(property).equals("2"))
//		       	   return "" + ("<img src='../images/baseimage/icon-complete.gif' />" );
		          return "" + ("<img src='images/baseimage/cut.png' />" );
		      // return SystemMessage.getstatus(model.get(property)+"");
		      }
		    };
		    busistatus.setRenderer(renderer) ;
		columns.add(busistatus);
		
		columns.add(new ColumnConfig("createdate", "日期", 100));
/*		ColumnConfig servicestartdate = new ColumnConfig("servicestartdate", "servicestartdate", 50);

		columns.add(servicestartdate);*/
		//ColumnModel cm = new ColumnModel(columns);

		// --set modetype
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.addField("busicode");
		type.addField("bcode");
		type.addField("ccode");
		type.addField("busistatus");
		type.addField("vprepare");
		type.addField("predate");
		type.addField("modifydate");
		type.addField("codename");
		type.addField("odate");
		type.addField("mark");

		type.addField("peoplenum");
		type.addField("Accounts");
		type.addField("Paid");
		type.addField("servicestartdate");
		type.addField("serviceenddate");
		type.addField("roomno");
		type.addField("s_cardno");
		type.addField("check_isas");
		type.addField("createdate");
		
		
		
		type.getField("Accounts").setType(Double.class);
		type.getField("Paid").setType(Double.class);
		type.getField("peoplenum").setType(Integer.class);
		type.getField("servicestartdate").setType(Date.class);
		type.getField("serviceenddate").setType(Date.class);
		type.getField("check_isas").setType(Boolean.class);
		
		
	
		
		
		StoreInfo info = new StoreInfo();
		info.setTablename("s_service_po");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select * from s_service_po order by createdate desc");
		// info.setKeyname(new String[] { "codename" });

		BaseBusiLeftPanel basbusileftpanel = new BaseBusiLeftPanel(columns,
				type, info,false,false)
		{
			protected boolean Select(Store<BusiObject> store,
					BusiObject parent, BusiObject record, String property,
					String filter) {
				for (int i = 0; i < type.getFieldCount(); i++) {
					
					Object name = record.get(type.getField(i).getName());
				//状态用了码名映射，所以要对名字进行搜索
					if(type.getField(i).getName().equals("busistatus"))
					{
						if(name!=null && SystemMessage.getstatus(name+"").indexOf(filter.toLowerCase()) !=-1)
						{
							return true;
						}
						
					}
					
					else if (name!=null&&name.toString().indexOf(filter.toLowerCase()) != -1) {
						return true;
					}
				}
				return false;
			}
		};
		store = basbusileftpanel.getStore();
		store.addDefault("busistatus","128");
		store.applyFilters("524288");
		basbusileftpanel.getGrid().setAutoExpandColumn("codename");
	//	setMaincomponent(basbusileftpanel.getGrid());
		return basbusileftpanel.getPanel();
	}
	/**
	 * 头部界面，主界面
	 */
	ContentPanel createService() {
		 service_panel = new ContentPanel();
		 
		 bfp = new BaseFormPanel(store);
		
		TableLayout tl = new TableLayout(4);
		tl.setWidth("600");
		
		bfp.setLayout(tl);
		bfp.setHeaderVisible(false);
		 
//		BaseFormPanel leftpanel = new BaseFormPanel(store);
//		leftpanel.setLayout(new FormLayout());
//		FormData fd = new FormData(200, 30);
//		leftpanel.setHeaderVisible(false);
//		leftpanel.setBodyBorder(false);
//
//		BaseFormPanel rightpanel = new BaseFormPanel(store);
//		rightpanel.setBodyBorder(false);
//		rightpanel.setHeaderVisible(false);

		 peoplenum = new NumberField();
		peoplenum.setName("peoplenum");
		peoplenum.setAllowBlank(false);
		peoplenum.setFieldLabel("服务人数");

		servicestartdate = new DateField();
		servicestartdate.setName("servicestartdate");
		servicestartdate.setAllowBlank(false);
		servicestartdate.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-M-d H:mm:ss"));  
		servicestartdate.setFieldLabel("服务开始时间");
		
		serviceenddate = new DateField();
		serviceenddate.setName("serviceenddate");
	//	serviceenddate.setPropertyEditor(propertyEditor)
		//n.setPropertyEditorType(Integer.class);
		serviceenddate.setFormatValue(true);
		//serviceenddate.setAllowBlank(false);
		serviceenddate.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-M-d H:mm:ss"));  
		serviceenddate.setFieldLabel("服务结束时间");
		
		 roomno = new TextField<String>();
		roomno.setName("roomno");
	//	roomno.setAllowBlank(false);
		roomno.setFieldLabel("房间号");

		Accounts = new NumberField();
		Accounts.setName("Accounts");
		Accounts.setAllowBlank(false);
//		Accounts.getPropertyEditor().set
//		Accounts.setPropertyEditor(propertyEditor)
//		setPropertyEditorType(Integer.class);
	//	Accounts.getPropertyEditor().
		Accounts.setFieldLabel("应收金额");
		Accounts.setAllowBlank(true);
		 Paid = new NumberField();
		Paid.setName("Paid");
		Paid.setAllowBlank(false);
		Paid.setFieldLabel("实收金额");
		
		TextField<String> icode = new TextField<String>();
		icode.setName("icode");
		icode.setAllowBlank(false);
		icode.setEnabled(false);
		icode.setFieldLabel("编号");

//		ModelType type = new ModelType();
//		type.setRoot("records");
//		type.setRecordName("record");
//
//		type.addField("code");
//
//		type.addField("name");
//
//		StoreInfo info = new StoreInfo();
//
//		info.setTablename("ccode");
//		info.setSql("select ccode as code,cname as name from ccode");
//		BaseStore<BaseModelData> ccodestore = new BaseStore<BaseModelData>(
//				type, info);
//		ccode = new BaseComboBoxForm("code",
//				"name", ccodestore);
		ccode = new TextField<String>();
		ccode.setFieldLabel("客户");
		ccode.setName("ccode");
		ccode.setEnabled(false);
		//ccode.setAllowBlank(false);
		
		
		 s_cardno = new TextField<String>();
		s_cardno.setName("s_cardno");
	//	roomno.setAllowBlank(false);
		s_cardno.setFieldLabel("会员卡号");
		
		// --------------

		
		 check_isas = new CheckBox();  
		check_isas.setBoxLabel("使用会员卡");  
		
		check_isas.setName("check_isas");
		// --------------

//		leftpanel.add(icode);
//		rightpanel.add(ccode);
//		leftpanel.add(peoplenum);
//		rightpanel.add(roomno);
//		leftpanel.add(servicestartdate);
//		rightpanel.add(serviceenddate);
//
//		leftpanel.add(Accounts);
//		rightpanel.add(Paid);
		
		TableData bfp_td = new TableData();
		bfp_td.setColspan(1);
		bfp_td.setRowspan(1);
		// 对齐方式
		bfp_td.setHorizontalAlign(HorizontalAlignment.LEFT);
		bfp_td.setVerticalAlign(VerticalAlignment.TOP);
		bfp_td.setPadding(5);
		
		
		//bfp.add(new Label("使用会员卡:"),bfp_td);
		bfp.add(check_isas,bfp_td);
		bfp.add(html,bfp_td);
		 aslabel = new Label("会员卡号:");
		bfp.add(aslabel,bfp_td);
		bfp.add(s_cardno,bfp_td);
		
		
		
		bfp.add(new Label("客户名:"),bfp_td);
		bfp.add(ccode,bfp_td);
		bfp.add(new Label("人数:"),bfp_td);
		bfp.add(peoplenum,bfp_td);
		bfp.add(new Label("房间号:"),bfp_td);
		bfp.add(roomno,bfp_td);
//		bfp.add(new Label("开始服务时间:"),bfp_td);
//		bfp.add(servicestartdate,bfp_td);
		bfp.add(new Label("结束服务时间:"),bfp_td);
		bfp.add(serviceenddate,bfp_td);
		bfp.add(new Label("应收款:"),bfp_td);
		bfp.add(Accounts,bfp_td);
		bfp.add(new Label("实收款:"),bfp_td);
		bfp.add(Paid,bfp_td);
		bfp.inited();
		/*
		 * leftpanel.add(codename); rightpanel.add(ccode);
		 * leftpanel.add(codename); rightpanel.add(ccode);
		 */

//		leftpanel.inited();
//		rightpanel.inited();
	
		
		TableData td = new TableData();
		td.setColspan(1);
		td.setRowspan(1);
		// 对齐方式
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);

		FieldSet fieldSet = new FieldSet();
		//fieldSet.setHeading("");
		
		fieldSet.setLayout(new TableLayout(1));
//		fieldSet.add(leftpanel, td);
//		fieldSet.add(rightpanel, td);
//		
		fieldSet.add(bfp, td);
		
//
	//	fieldSet.add(hpanel, td);

		final BorderLayout layout = new BorderLayout();
		service_panel.setHeaderVisible(false);
		service_panel.setLayout(layout);
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(5, 0, 5, 0));
		service_panel.add(fieldSet, centerData);
		// panel.setAutoHeight(true);
		service_panel.setHeight("200");
		//service_panel.setWidth("600");
		service_panel.setAutoWidth(true);

		
		return service_panel;

	}

	/**
	 * 服务类型
	 * 
	 * @return
	 */
	BaseGrid createServiceItem() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

	
		// ---------------------------1
		// ---------------
		List<ColumnConfig> bocodecolumns = new ArrayList<ColumnConfig>();
		ColumnConfig service = new ColumnConfig("service", "人员编码", 80);

		bocodecolumns.add(service);

		ColumnConfig price = new ColumnConfig("price", "单价", 50);

		bocodecolumns.add(price);
		ColumnConfig priceunit = new ColumnConfig("priceunit", "单位", 50);

		bocodecolumns.add(priceunit);

		ModelType combotype = new ModelType();
		combotype.setRoot("records");
		combotype.setRecordName("record");
		combotype.setTotalName("totle");
		// combotype.addField("code", "service");
		combotype.addField("service");
		combotype.addField("priceunit");
		combotype.addField("price");
		combotype.addField("bcode");
		StoreInfo comboinfo = new StoreInfo();
		comboinfo.setTablename("s_service");
		comboinfo.setSql("select service,priceunit,price from s_service");
		BaseGridEditor bge = new BaseGridEditor(bocodecolumns, combotype,
				comboinfo, "service");
		

		ColumnConfig serviceitem = new ColumnConfig("serviceitem", "服务类型", 80);
		//TextField<String> serviceitemtext = new TextField<String>();
		GridEditor grideditor = new GridEditor(bge);
	//	serviceitem.setEditor(grideditor);
		serviceitem.setRenderer(grideditor);
		// serviceitem.setEditor(editor);
		// serviceitem.setEditor(new CellEditor(dateField));
		// serviceitem.setDateTimeFormat(DateTimeFormat.getMediumDateFormat());
		
		ColumnConfig method = new ColumnConfig("method", "指定方式", 80);
		CellEditorFactory  cef = new CellEditorFactory();
		CellEditor ce1  = cef.getMethodCellEditor();
		//method.setEditor(ce1);
		
		
		

		ColumnConfig qty = new ColumnConfig("qty", "数量",50);
		qty.setAlignment(HorizontalAlignment.RIGHT);
		//qty.setNumberFormat(NumberFormat.getFormat("#,##0"));
		NumberField n = new NumberField();
		n.setFormat(NumberFormat.getFormat("#,##0"));
		n.setPropertyEditorType(Integer.class);
		n.setName("qty");
		
	//	qty.setEditor(new CellEditor(n));
		//qty.getEditor().addListener(Events.Complete, this);
		
		ColumnConfig upric = new ColumnConfig("upric", "单价", 50);
		// TextField<String> uprictext = new TextField<String>();
		// upric.setEditor(new CellEditor(uprictext));
		upric.setAlignment(HorizontalAlignment.RIGHT);
		upric.setNumberFormat(NumberFormat.getCurrencyFormat());
	//	upric.setEditor(new CellEditor(new NumberField()));
	//	upric.getEditor().getField().setName("upric");
		
		//upric.getEditor().addListener(Events.Complete, this);
	
		ColumnConfig qtyunit = new ColumnConfig("qtyunit", "单位", 50);
		TextField<String> qtyunittext = new TextField<String>();
	//	qtyunit.setEditor(new CellEditor(qtyunittext));
		
		
		ColumnConfig total = new ColumnConfig("total", "小计", 100);
		// TextField<String> uprictext = new TextField<String>();
		// upric.setEditor(new CellEditor(uprictext));
		total.setAlignment(HorizontalAlignment.RIGHT);
		total.setNumberFormat(NumberFormat.getCurrencyFormat());
		//total.setEditor(new CellEditor(new NumberField()));
		
		

			
		
		 //**********************************
			ColumnConfig c9 = new ColumnConfig("bcodeshortcode", "技师编号", 80);
			TextField<String> c9text = new TextField<String>();
			//c9.setEditor(new CellEditor(c9text));
			c9text.setName("bcodeshortcode");
			c9text.setFireChangeEventOnSetValue(true);
			
			
			//c9.getEditor().getField().addListener(Events.Change, this);
			
			ColumnConfig c10 = new ColumnConfig("bname", "技师名", 50);
			TextField<String> c10text = new TextField<String>();
		//	c10.setEditor(new CellEditor(c10text));
			c10text.setName("bname");
//		columns.add(bcode);
			columns.add(c9);
			columns.add(c10);
		columns.add(method);
		columns.add(serviceitem);
		
		columns.add(qty);
		columns.add(upric);
		columns.add(qtyunit);
		columns.add(total);
		
		
		
		ModelType type = new ModelType();

		type.setRoot("records");
		type.setRecordName("record");

		type.addField("idx");
		type.addField("icode");

		type.addField("serviceitem");

		type.addField("qty");

		type.addField("qtyunit");
		type.addField("upric");
		type.addField("bcode");
		type.addField("method");
		type.addField("total");
		type.addField("bcodeshortcode");
		type.addField("bname");
		type.getField("qty").setType(Integer.class);
		type.getField("total").setType(Double.class);
		type.getField("upric").setType(Double.class);
		type.getField("bcode").setType(String.class);
		
		
		StoreInfo info = new StoreInfo();

		info.setTablename("s_service_po_g");
		info.setSql("select * from s_service_po_g where upicode='{icode}'");
		info.setParent(store);// 关联主表
		// info.setAutoinccolumn("idx"); // 自增字段，如果子表一定需要
		info.setLinkparentcolumns(new String[] { "icode" }); // 关联主表字段
		info.setLinkparent_chile_columns(new String[] { "upicode" });// 关联主表字段
																		// (子表)

		info.setKeyname(new String[] { "icode" }); // 主键

		s_service_po_g_store = new BaseStore<BusiObject>(type, info);
		s_service_po_g_store.setName("s_service_po_g");
		
	
		ColumnModel cm = new ColumnModel(columns);

		AggregationRowConfig averages = new AggregationRowConfig();
		averages.setHtml("serviceitem", "合计：");

		averages.setSummaryType("total", SummaryType.SUM);
		averages.setSummaryFormat("total", NumberFormat.getCurrencyFormat());
		
		 cm.addAggregationRow(averages);

		bge.setTarget_store(s_service_po_g_store);
		bge.setSourcecol(new ColumnInfo[] { new ColumnInfo("priceunit",ColumnInfo.STRING), new ColumnInfo("price",ColumnInfo.DOUBLE) });
		bge.setTarget_col(new ColumnInfo[] { new ColumnInfo("qtyunit",ColumnInfo.STRING), new ColumnInfo("upric",ColumnInfo.DOUBLE) });
		//		
		 s_service_po_g_grid = new BaseGrid(s_service_po_g_store, cm,false,false,null);
		// s_service_po_g_grid.setEnabled(false);
		// s_service_po_g_grid.mask("sss", "base-x-item-disabled");
		// GroupSummaryView summary = new GroupSummaryView();
		cbs.setSelectionMode(SelectionMode.SINGLE);
		
		s_service_po_g_grid.setSelectionModel(cbs);
	//	s_service_po_g_grid.setAutoHeight(true);

		s_service_po_g_grid.setAutoWidth(true);
		s_service_po_g_grid.setHeight(200);
	
		// bcgp = new
		// BaseChileGridPanel(type,info,columns,BaseChileGridPanel.QUERY_MODE);
		//s_service_po_g_store.addBaseStoreListener(BaseStoreEvent.EventType.CELL_CHANGED, this);
	
		return s_service_po_g_grid;
	}
}
